package cn.edu.xmu.oomall.customer.service;

import cn.edu.xmu.oomall.core.model.VoObject;
import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.dao.CustomerDao;
import cn.edu.xmu.oomall.customer.model.bo.Customer;
import cn.edu.xmu.oomall.customer.model.po.CustomerPo;
import cn.edu.xmu.oomall.customer.model.vo.CustomerRetVo;
import cn.edu.xmu.oomall.customer.model.vo.SimpleCustomerRetVo;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
/**
 * @author Jiazhe Yuan
 * @date 2021-12-14 12:40:31
 * @modifiedBy Jiazhe Yuan
 * @date 2021-12-14 13:25:18
 */
public class CustomerService {

    @Autowired
    private CustomerDao customerDao;

    public ReturnObject getAllCustomerStates() {
        List<Map<String, Object>> stateList = new ArrayList<>();
        for(Customer.State state : Customer.State.values()) {
            Map<String, Object> tmp = new HashMap<>(16);
            tmp.put("code", state.getCode());
            tmp.put("name", state.getDescription());
            stateList.add(tmp);
        }
        return new ReturnObject(stateList);
    }

    @Transactional(rollbackFor = Exception.class)
    public ReturnObject<PageInfo<VoObject>> getAllCustomers(String username, String email, String mobile, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        PageInfo<CustomerPo> customerPos = customerDao.getAllCustomers(username, email, mobile);

        List<VoObject> customers = customerPos.getList().stream().map(SimpleCustomerRetVo::new).collect(Collectors.toList());

        PageInfo<VoObject> returnObject = new PageInfo<>(customers);
        returnObject.setPages(customerPos.getPages());
        returnObject.setPageSize(customerPos.getPageSize());
        returnObject.setPageNum(customerPos.getPageNum());
        returnObject.setTotal(customerPos.getTotal());
        return new ReturnObject<>(returnObject);
    }

    @Transactional(rollbackFor = Exception.class)
    public ReturnObject getCustomerByAdminWithId(Long id) {
        ReturnObject returnObject = customerDao.getCustomerById(id);
        CustomerPo customerPo = (CustomerPo) returnObject.getData();
        if (null != customerPo) {
            CustomerRetVo customerRetVo = Common.cloneVo(customerPo, CustomerRetVo.class);
            return new ReturnObject(customerRetVo);
        } else {
            return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public ReturnObject getCustomerInternal(Long id) {
        ReturnObject returnObject = customerDao.getCustomerById(id);
        CustomerPo customerPo = (CustomerPo) returnObject.getData();
        if (null != customerPo) {
            SimpleCustomerRetVo simpleCustomerRetVo = Common.cloneVo(customerPo, SimpleCustomerRetVo.class);
            return new ReturnObject(simpleCustomerRetVo);
        } else {
            return new ReturnObject(ReturnNo.RESOURCE_ID_NOTEXIST);
        }
    }
}
